const { series, parallel, src, dest, task } = require('gulp');
const watch = require('gulp-watch');

const config = require('./config');
const del = require('del');
const rename = require('gulp-rename');

const asciidoc = require('./tasks/asciidoctor');

task('build-adoc', function () {
  return src(config.blogDir + '/**/*.adoc')
    .pipe(asciidoc())
    .pipe(rename({ extname: '.html' }))
    .pipe(dest(config.deployDir + '/'));
});

task('cname', require('./tasks/cname'));
task('clean', (cb) => del([config.deployDir + '/**/*', config.deployDir, '.db.json']));
task('copy-assets', () => src(config.resourceDir + '/assets/**/*.*').pipe(dest(config.deployDir + '/assets/')));
task('home', require('./tasks/home'));
task('server', require('./tasks/server'));
task('style', require('./tasks/style'));
task('tags', require('./tasks/tags'));

task('watch-adoc', function () {
  return watch(config.blogDir + '/**/*.adoc')
    .pipe(asciidoc())
    .pipe(rename({ extname: '.html' }))
    .pipe(dest(config.deployDir + '/'));
});

task('watch-style', function () {
  // TODO: 观察 style 变化
});

exports.default = series(
  'clean',
  parallel('build-adoc', 'style', 'copy-assets'),
  parallel('home', 'tags', 'cname')
);
exports.serve = series(
  'clean',
  parallel('build-adoc', 'style', 'copy-assets'),
  parallel('home', 'tags', 'cname'),
  parallel('watch-adoc', 'server'),
); 